package cn.itcast.springmvc.controller;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.itcast.springmvc.pojo.Items;
import cn.itcast.springmvc.pojo.QueryVo;
import cn.itcast.springmvc.service.ItemService;

import org.springframework.web.servlet.ModelAndView;

@Controller
public class ItemsController {

	@Autowired
	private ItemService itemService;
	
	
	
	@RequestMapping("/itemList.action")
	public ModelAndView getItemList(){
		List<Items> itemsList = itemService.getItemList();
		ModelAndView view = new ModelAndView();
		view.setViewName("itemList");
		view.addObject("itemList", itemsList);
		return view;
	}
	
	
	/**
	 * 修改操作
	 * @param request
	 * @param response
	 * @param session
	 * @return
	 */
	/*@RequestMapping("/itemEdit.action")
	public ModelAndView skipToEdit(HttpServletRequest request,HttpServletResponse response,HttpSession session){
		ModelAndView view = new ModelAndView();
		String parameter = request.getParameter("id");
		System.out.println(parameter);
		//通过id来查询items对象，然后跳转到editItem.jsp页面上去，将查询出来的数据带过去即可
		return view;
	}*/
	
	
	/*@RequestMapping("/itemEdit.action")
	public ModelAndView skipToEdit(Integer id){
		ModelAndView view = new ModelAndView();
		System.out.println(id);
		Items items = itemService.selectByPrimayKey(id);
		
		view.addObject("item", items);
		view.setViewName("editItem");
		//通过id来查询items对象，然后跳转到editItem.jsp页面上去，将查询出来的数据带过去即可
		return view;
	}*/
	
	
	//
	@RequestMapping("/itemEdit.action")
	public String skipToEdit(/*@RequestParam(value="ids" ,defaultValue="2",required=true)*/ Integer id,Model model,ModelMap modelMap){
		System.out.println(id);
		Items items = itemService.selectByPrimayKey(id);
		model.addAttribute("item", items);
		/*model.addObject("item", items);
		model.setViewName("editItem");*/
		//通过id来查询items对象，然后跳转到editItem.jsp页面上去，将查询出来的数据带过去即可
		return "editItem";
	}
	
	
	
	
	
	/**
	 * 修改操作，绑定pojo对象
	 */
	@RequestMapping("/updateitem.action")
	public String updateItem(HttpServletRequest request,Items items){
		String name = request.getParameter("name");
		String price = request.getParameter("price");
		String detail = request.getParameter("detail");
		String id = request.getParameter("id");
		System.out.println(name+price+detail+"......."+id);
		
		System.out.println(items.toString());
		
		itemService.updateItem(items);
		
		
		return "success";
	}
	
	
	@RequestMapping("/queryitem.action")
	public String queryItem(QueryVo queryVo){
		
		System.out.println(queryVo.getItems().getName());
		
		System.out.println(queryVo.getItems().getDetail());
		
		return "success";
	}
	
	
	
	
	
	

}
